package net.sochk.web.handler;

import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sochk.web.entity.Menu;
import net.sochk.web.entity.User;
import net.sochk.web.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MenuHandler {

	@Autowired
	private MenuService menuService;

	@Autowired
	private HttpServletRequest request;

	public MenuService getMenuService() {
		return this.menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	@ResponseBody
	@RequestMapping(value = { "/loading" }, produces = { "application/json;charset=UTF-8" })
	public String load(@RequestParam("user") String name, @RequestParam("password") String password) {
		return this.menuService.loading(name, password);
	}

	@ResponseBody
	@RequestMapping(value = { "hehe" }, produces = { "application/json; charset=utf-8" })
	public String load1() {
		String haha = "��֪��";
		return haha;
	}

	@ResponseBody
	@RequestMapping(value = { "/getAllMenu" }, produces = { "application/json;charset=UTF-8" })
	public String getAllMenu() {
		return this.menuService.getAllMenuString();
	}

	@ResponseBody
	@RequestMapping(value = { "/getAllMenu1" }, produces = { "application/json;charset=UTF-8" })
	public String getAllMenu1(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("/NewFile");
		mav.addObject("account", this.menuService.getproduct());
		String[] strings = new String[this.menuService.getproduct().size()];
		String[] string = new String[this.menuService.getphoneproduct().size()];
		this.menuService.getproduct().toArray(strings);

		request.setAttribute("gg", strings);
		request.setAttribute("cc", string);

		return this.menuService.getAllMenuString();
	}

	@ResponseBody
	@RequestMapping({ "getAllFirstMenu" })
	public List<Menu> getAllFirstMenu() {
		System.out.println(this.menuService.getAllFirstMenu());
		return this.menuService.getAllFirstMenu();
	}

	@ResponseBody
	@RequestMapping({ "getAllSecondMenu" })
	public List<Menu> getAllSecondMenu(@RequestParam("MenuId") int MenuId) {
		System.out.println(this.menuService.getAllSecondMenu(MenuId));
		return this.menuService.getAllSecondMenu(MenuId);
	}

	@ResponseBody
	@RequestMapping({ "getproduct" })
	public List<String> getproduct() {
		System.out.println(this.menuService.getproduct());

		return this.menuService.getproduct();
	}

	@ModelAttribute
	public void gethaha(@RequestParam(value = "menuId", required = false) Integer id, Map<String, Object> map) {
		if (id != null)
			map.put("menu", this.menuService.getOne(id));
	}

	@ModelAttribute
	public void gethehe(@RequestParam(value = "adminId", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			System.out.println(id + "aaa");
			map.put("user", this.menuService.getAdmin(id.intValue()));
		}
	}

	@ResponseBody
	@RequestMapping(value = { "/Updata" }, produces = { "test/html; charset=utf-8" })
	public String Updata(HttpServletRequest request, Menu menu, @RequestParam("photofile") MultipartFile file,
			@RequestParam("zipfile") MultipartFile zip, @RequestParam("zipfile") MultipartFile zip1) {
		String aa = request.getSession().getServletContext().getRealPath("/");
		System.out.println(aa);

		String news = this.menuService.Updata(menu, file, zip, zip1, request, aa);
		System.out.println(aa + "aa");
		System.out.println(news);
		return news;
	}

	@ResponseBody
	@RequestMapping(value = { "/AddOrUpdata" }, produces = { "test/html; charset=utf-8" })
	public String AddOrUpdata(HttpServletRequest request, Menu menu, @RequestParam(value="photofile",required=false) MultipartFile file,
			@RequestParam(value="zipfile",required=false) MultipartFile zip, @RequestParam(value="zipfile1",required=false) MultipartFile zip1) {
		if ((menu.getMenuName().isEmpty()) && (file.isEmpty()) && (zip.isEmpty()) && (zip1.isEmpty())) {
			return "null";
		}
		String path = request.getSession().getServletContext().getRealPath("/");
		System.out.println(path + "aa");
		String news = this.menuService.AddOrUpdata(menu, file, zip, zip1, request, path);
		return news;
	}

	@ResponseBody
	@RequestMapping(value = { "/DeleteMeau" }, produces = { "application/json;charset=UTF-8" })
	public String DeleteMeau(@RequestParam("id") int id,HttpServletRequest request) {
		String path = request.getSession().getServletContext().getRealPath("/");
		this.menuService.downallOrder(id);
		this.menuService.DeleteMeau(id,path);
		return this.menuService.getAllMenuString1();
	}

	@ResponseBody
	@RequestMapping({ "getAllAdmin" })
	public List<User> getAllAdmin() {
		return this.menuService.getAllAdmin();
	}

	@ResponseBody
	@RequestMapping({ "getAdmin" })
	public User getAdmin(int id) {
		return this.menuService.getAdmin(id);
	}

	@ResponseBody
	@RequestMapping(value = { "/AddOrUpdatauser" }, produces = { "test/html;charset=UTF-8" })
	public String AddOrUpdata(User user) {
		String aa = this.menuService.AddOrUpdata(user);
		return aa;
	}

	@ResponseBody
	@RequestMapping(value = { "/Updatauser" }, produces = { "test/html;charset=UTF-8" })
	public String Updata(User user, @RequestParam("adminpassword") String password,
			@RequestParam("adminuser") String adminuser) {
		String aa = this.menuService.Updata(user, password, adminuser);
		return aa;
	}

	@ResponseBody
	@RequestMapping({ "DeleteUser" })
	public void DeleteUser(@RequestParam("adminId") int id) {
		this.menuService.DeleteUser(id);
	}

	@ResponseBody
	@RequestMapping({ "/show1" })
	public ModelAndView show1(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		ModelAndView mav = new ModelAndView("redirect:frame/index.jsp");
		//mav.addObject("account", this.menuService.getproduct());
		String[] strings = new String[this.menuService.getproduct().size()];
		String[] string = new String[this.menuService.getphoneproduct().size()];
		this.menuService.getproduct().toArray(strings);
		session.setAttribute("gg", strings);
		session.setAttribute("cc", string);

		return mav;
	}

	@RequestMapping({ "/show2" })
	public ModelAndView show2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		ModelAndView mav = new ModelAndView("redirect:frame_phone/index.jsp");
		mav.addObject("account", this.menuService.getproduct());
		String[] strings = new String[this.menuService.getproduct().size()];
		String[] string = new String[this.menuService.getphoneproduct().size()];
		this.menuService.getproduct().toArray(strings);
		this.menuService.getphoneproduct().toArray(string);

		session.setAttribute("gg", strings);

		session.setAttribute("cc", string);

		return mav;
	}

	@ResponseBody
	@RequestMapping(value = { "/upOrder" }, produces = { "application/json; charset=utf-8" })
	public String upOrder(@RequestParam("id") int id) {
		this.menuService.upOrder(id);
		return this.menuService.getAllMenuString1();
	}

	@ResponseBody
	@RequestMapping(value = { "/downOrder" }, produces = { "application/json; charset=utf-8" })
	public String downOrder(@RequestParam("id") int id) {
		this.menuService.downOrder(id);
		return this.menuService.getAllMenuString1();
	}

	@RequestMapping({ "/home" })
	public ModelAndView home(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		ModelAndView mav = new ModelAndView("redirect:first.jsp");
		System.out.println("ff");
		return mav;
	}

	@RequestMapping({ "/login" })
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		ModelAndView mav = new ModelAndView("redirect:ProjectCMS2/onload.html");

		return mav;
	}
}